package com.wjj.happytime.adapter.listen_frg_adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.jiongbull.jlog.JLog;
import com.squareup.picasso.Picasso;
import com.wjj.happytime.R;
import com.wjj.happytime.activity.listen_activity.NomalRadioListActivity;
import com.wjj.happytime.activity.listen_activity.PlayActivity;
import com.wjj.happytime.activity.listen_activity.ProvinceRadioActivity;
import com.wjj.happytime.adapter.listen_frg_adapter.ListenFrgMainAdapter.ItemViewHolder;
import com.wjj.happytime.entity.listen_entity.HomePageRadioDetail;
import com.wjj.happytime.entity.listen_entity.ListenHomePage;
import com.wjj.happytime.entity.listen_entity.ListenHomePageData;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by junjie.wang on 2017/4/20.
 */

public class ListenFrgMainAdapter extends Adapter<ItemViewHolder> implements OnClickListener {

  private final int TYPERADIOS = 1;//头部四个电台
  private final int TYPEITEMHEAD = 2;//条目的头
  private final int TYPEITEM = 3;//每个条目

  private Context context;
  public int itemCount=9;//听页面的条目数
  public LayoutInflater inflate;
  private  View view ;
  //本地电台
  public List<HomePageRadioDetail> localRadios;
  //热门电台
  public List<HomePageRadioDetail> topRadios;
  //地名
  public String location;

  public ListenFrgMainAdapter(Context context,ListenHomePage data) {
    this.context = context;
    ListenHomePageData homePageData = data.getData();
    localRadios = homePageData.getLocalRadios();
    topRadios = homePageData.getTopRadios();
    location = homePageData.getLocation();
  }

  @Override
  public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    ItemViewHolder holder = null;
    inflate = LayoutInflater.from(context);
    switch (viewType) {
      case TYPERADIOS:
        view = inflate.inflate(R.layout.frg_listen_item_first, parent, false);
        holder = new ItemViewHolder(view, TYPERADIOS);
        break;
      case TYPEITEMHEAD:
        view = inflate.inflate(R.layout.frg_live_peritem_head, parent, false);
        holder = new ItemViewHolder(view, TYPEITEMHEAD);
        break;
      case TYPEITEM:
        view = inflate.inflate(R.layout.frg_live_peritem, parent, false);
        holder = new ItemViewHolder(view, TYPEITEM);
        break;
    }
    return holder;
  }

  @Override
  public void onBindViewHolder(ItemViewHolder holder, int position) {
    if (position == 0) {//头部4个电台
      holder.tvRadioLocal.setOnClickListener(this);
      holder.tvRadioCountry.setOnClickListener(this);
      holder.tvRadioProvince.setOnClickListener(this);
      holder.tvRadioNet.setOnClickListener(this);

    } else if (position == 1) {//本地电台条目的头
      if (holder.tvItemHead==null) {
        JLog.e("holder.tvItemHead==null");
      }else {
        JLog.e(location);
      }

      holder.tvItemHead.setText(location);
      holder.tvMore.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
          Intent intent = new Intent(context,NomalRadioListActivity.class);
          intent.putExtra("type",R.id.tv_radio_local);
          context.startActivity(intent);
        }
      });
    }else if (position==5){//Top电台条目的头
      holder.tvMore.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
          Intent intent = new Intent(context,NomalRadioListActivity.class);
          intent.putExtra("type",5);
          intent.putExtra("topRadios", (Serializable) topRadios);
          context.startActivity(intent);
        }
      });
    }
    else if (position==9){//收听历史条目的头
      holder.tvItemHead.setText("收听历史");
      holder.tvMore.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
          Intent intent = new Intent(context,NomalRadioListActivity.class);
          intent.putExtra("type",9);
          context.startActivity(intent);
        }
      });
    }
    else {//条目
      switch (position) {
        case 2:
        case 3:
        case 4:
          final HomePageRadioDetail localRadioDetail = localRadios.get(position-2);
          Picasso.with(context).load(localRadioDetail.getCoverLarge()).into(holder.ivItemPic);
          holder.tvRadioName.setText(localRadioDetail.getName());
          holder.tvProgramName.setText(localRadioDetail.getProgramName());
          holder.tvPlayCount.setText(localRadioDetail.getPlayCount()+"");
          holder.ivPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent intent = new Intent(context, PlayActivity.class);
              intent.putExtra("playUrl",localRadioDetail.getPlayUrl().getTs64());
              intent.putExtra("radioName",localRadioDetail.getName());
              context.startActivity(intent);
            }
          });
          break;
        case 6:
        case 7:
        case 8:
          final HomePageRadioDetail topRadioDetail = topRadios.get(position-6);
          Picasso.with(context).load(topRadioDetail.getCoverLarge()).into(holder.ivItemPic);
          holder.tvRadioName.setText(topRadioDetail.getName());
          holder.tvProgramName.setText(topRadioDetail.getProgramName());
          holder.tvPlayCount.setText(topRadioDetail.getPlayCount()+"");
          holder.ivPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent intent = new Intent(context, PlayActivity.class);
              intent.putExtra("playUrl",topRadioDetail.getPlayUrl().getTs64());
              intent.putExtra("radioName",topRadioDetail.getName());
              context.startActivity(intent);
            }
          });
          break;
        default://收听历史
          break;
      }

    }

  }

  @Override
  public int getItemCount() {
    return itemCount;
  }

  //根据位置来判断type
  @Override
  public int getItemViewType(int position) {
    if (position == 0) {
      return TYPERADIOS;
    } else if (position == 1 || position == 5 || position == 9) {
      return TYPEITEMHEAD;
    } else {
      return TYPEITEM;
    }
  }

  //头部四个广播点击事件
  @Override
  public void onClick(View view) {
    Intent intent = new Intent();
    switch (view.getId()) {
      case R.id.tv_radio_local:
      case R.id.tv_radio_country:
      case R.id.tv_radio_internet:
        intent.setClass(context, NomalRadioListActivity.class);
        intent.putExtra("type",view.getId());
        break;
      case R.id.tv_radio_province:
        intent.setClass(context, ProvinceRadioActivity.class);
        break;
    }
    context.startActivity(intent);

  }

  public class ItemViewHolder extends RecyclerView.ViewHolder {

    //TYPERADIOS
    public TextView tvRadioLocal;
    public TextView tvRadioCountry;
    public TextView tvRadioProvince;
    public TextView tvRadioNet;

    //TYPEITEMHEAD
    public TextView tvItemHead;
    public TextView tvMore;

    //TYPEITEM
    public ImageView ivItemPic;
    public TextView tvRadioName;
    public TextView tvProgramName;
    public TextView tvPlayCount;
    public ImageView ivPlay;


    public ItemViewHolder(View itemView, int viewType) {
      super(itemView);
      JLog.e(viewType+"");
      switch (viewType) {

        case TYPERADIOS:
          tvRadioLocal = ((TextView) itemView.findViewById(R.id.tv_radio_local));
          tvRadioCountry = ((TextView) itemView.findViewById(R.id.tv_radio_country));
          tvRadioProvince = ((TextView) itemView.findViewById(R.id.tv_radio_province));
          tvRadioNet = ((TextView) itemView.findViewById(R.id.tv_radio_internet));
          break;
        case TYPEITEMHEAD:
          tvItemHead = ((TextView) itemView.findViewById(R.id.tv_live_peritem_head));
          tvMore = ((TextView) itemView.findViewById(R.id.tv_more));
          break;
        case TYPEITEM:
          ivItemPic = ((ImageView) itemView.findViewById(R.id.iv_peritem_id));
          tvRadioName = ((TextView) itemView.findViewById(R.id.tv_peritem_radioname_id));
          tvProgramName = ((TextView) itemView.findViewById(R.id.tv_peritem_programName_id));
          tvPlayCount = ((TextView) itemView.findViewById(R.id.tv_peritem_playCount_id));
          ivPlay = ((ImageView) itemView.findViewById(R.id.iv_play));
          break;
      }
    }
  }

}
